//
//  SelectDatePick.m
//  LetterTourBooks
//
//  Created by min yang on 14-6-20.
//  Copyright (c) 2014年 zhoujia. All rights reserved.
//

#import "SelectDatePick.h"

@interface SelectDatePick()
@property (nonatomic,strong) UIDatePicker * ipicker;
@property (nonatomic, strong) UIToolbar *toolBar;
@end

@implementation SelectDatePick
@synthesize ipicker;


- (instancetype)init:(datePickerBlock)block{
    Myblock = block;
    self = [super init];
    if (self) {
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        self.bounds = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        ipicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 250, [UIScreen mainScreen].bounds.size.width, 250)];
        ipicker.backgroundColor = [UIColor whiteColor];
        [ipicker setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
        [ipicker setDatePickerMode:UIDatePickerModeDate];
        [ipicker setDate:[NSDate date] animated:YES];
        // 设置显示最大时间（此处为当前时间）
        //[ipicker setMaximumDate:[NSDate date]];
        // 设置最小时间
        NSDate *addDate = [NSDate date];//这里设置1 没有反应。设置2可以提前一天
        [ipicker setMaximumDate:addDate];
        [self addSubview:ipicker];
        [self addSubview:self.toolBar];
    }
    return self;
}


- (UIToolbar *)toolBar{
    if (_toolBar == nil) {
        self.toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 294, [UIScreen mainScreen].bounds.size.width, 44)];
        _toolBar.barTintColor=[UIColor whiteColor];
        UIBarButtonItem *item=[[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(click:)];
        item.tag = 120;
        UIBarButtonItem *item1=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        UIBarButtonItem *item2=[[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(click:)];
        item2.tag = 121;
        _toolBar.items = @[item,item1, item2];
    }
    return _toolBar;
}

- (void)click:(UIBarButtonItem*)sender
{
    if (sender.tag == 120){
        [self animatedOut];
    }
    if (sender.tag == 121)
    {
        NSDate * seledate = [ipicker date];
        NSDateFormatter * fom = [[NSDateFormatter alloc]init];
        [fom setDateFormat:@"yyyy"];
        NSString * datestr = [fom stringFromDate:seledate];
        if (Myblock)
        {
            Myblock(datestr);
        }
        [self animatedOut];
        
    }
}

#pragma mark - Animated Mthod
- (void)animatedIn{
    self.transform = CGAffineTransformMakeScale(1.3, 1.3);
    self.alpha = 0;
    [UIView animateWithDuration:.35 animations:^{
        self.alpha = 1;
        self.transform = CGAffineTransformMakeScale(1, 1);
    }];
}

- (void)animatedOut
{
    [UIView animateWithDuration:.35 animations:^{
        self.transform = CGAffineTransformMakeScale(1.3, 1.3);
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }
    }];
}

- (void)dismiss
{
    [self animatedOut];
}

@end
